package cn.cmcc.intertelecom.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.cmcc.intertelecom.bean.Department;
import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.bean.User;
import cn.cmcc.intertelecom.service.DepartmentService;
import cn.cmcc.intertelecom.service.UserService;
import cn.cmcc.intertelecom.utils.ArchivesLog;

@Controller
public class DepartmentController {
	@Autowired
	private DepartmentService deptService;
	@Autowired
	private UserService userService;
	
	
	/**
	 * 显示部门
	 * @param department
	 * @param result
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dept/{id}" ,method=RequestMethod.GET)
	public Msg getDeptById(@PathVariable(value="id")Integer id){
		
		Department department=deptService.selectDeptById(id);
		List<User> userList = userService.getUsersByDeptId(id);
		return Msg.success()
				.add("userList", userList)
				.add("dept", department);
	}

	
	/**
	 * 删除用户
	 * @param department
	 * @param result
	 * @return
	 */
	@ArchivesLog(operationPower="delete_dept")
	@ResponseBody
	@RequestMapping(value="/dept/{deptIds}" ,method=RequestMethod.DELETE)
	public Msg deleteDept(@PathVariable(value="deptIds")String deptIds){
		int i=0;
		if(deptIds.contains("-")){
			String[] str = deptIds.split("-");
			List<Integer> ids=new ArrayList<>();
			for (String string : str) {
				ids.add(Integer.parseInt(string));
			}
			deptService.deleteBatch(ids);
		}else{
			i=deptService.deleteDept(Integer.parseInt(deptIds));
		}
		System.out.println(i);
		return Msg.success();
	}
	
	/**
	 * 更新部门
	 * @param department
	 * @param result
	 * @return
	 */
	@ArchivesLog(operationPower="update_dept")
	@ResponseBody
	@RequestMapping(value="/dept" ,method=RequestMethod.PUT)
	public Msg updateDept(@Valid Department department,BindingResult result){
		
		if(result.hasErrors()){
			//校验失败，应该返回失败，在模态框中显式校验失败的错误消息
			Map<String, Object> map=new HashMap<>();
			List<FieldError> error = result.getFieldErrors();
			for (FieldError fieldError : error) {
				System.out.println("错误消息："+fieldError.getDefaultMessage());
				map.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			
			return Msg.fail().add("fieldError", map);
		}else{
			deptService.updateDept(department);
			return Msg.success();
		}
	}
	
	/**
	 * 保存部门信息
	 * @param department
	 * @param result
	 * @return
	 */
	@ArchivesLog(operationPower="new_dept")
	@ResponseBody
	@RequestMapping(value="/dept" ,method=RequestMethod.POST)
	public Msg saveDept(@Valid Department department,BindingResult result){
		
		if(result.hasErrors()){
			//校验失败，应该返回失败，在模态框中显式校验失败的错误消息
			Map<String, Object> map=new HashMap<>();
			List<FieldError> error = result.getFieldErrors();
			for (FieldError fieldError : error) {
				System.out.println("错误消息："+fieldError.getDefaultMessage());
				map.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			
			return Msg.fail().add("fieldError", map);
		}else{
			deptService.saveDept(department);
			return Msg.success();
		}
	}

	/**
	 * 显示所有部门
	 * @param pn
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/depts",method=RequestMethod.GET)
	public  Msg deptList(@RequestParam(value="pn",defaultValue="1") Integer pn){
		
		//获取第n页，m条内容，默认查询总数count
		PageHelper.startPage(pn, 5);
		List<Department> list = deptService.selectAllDeptAndAdmin();
		//用PageInfo对结果进行包装
		PageInfo<Department> page = new PageInfo<>(list,5);
		
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * 获取所有的部门  仅包括部门  用于新建用户时下拉框的显示
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deptGroup",method=RequestMethod.GET)
	public Msg deptList(){
		
		List<Department> list = deptService.selectAllDepts();
		
		return Msg.success().add("list", list);
	}
	
	
	
	/**
	 * 用于跳转页面，跳转到department/department-list.jsp
	 */
	/*@RequestMapping(value="/todeptList",method=RequestMethod.GET)
	public  String todeptList(Map<String,Object> map){
		
		//获取第1页，5条内容，默认查询总数count
		PageHelper.startPage(1, 5);
		List<Department> list = deptService.selectAll();
		//用PageInfo对结果进行包装
		PageInfo<Department> page = new PageInfo<>(list,5);
		map.put("pageInfo", page);
		return "department/department-list";
	}*/
}
